package com.ruoyi.web.controller.encryption;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.custom.domain.encryption.EncryptEnvconfig;
import com.ruoyi.custom.domain.encryption.vo.EncryptEnvconfigVo;
import com.ruoyi.custom.service.encryption.IEncryptEnvconfigService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/encryption/envConfig")
public class EnvConfigController extends BaseController {

    @Resource
    private IEncryptEnvconfigService envconfigService;

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('encryption:envconfig:add')")
    @Log(title = "加解密环境配置", businessType = BusinessType.INSERT)
    @PostMapping("addEnvConfig")
    public AjaxResult addEnvConfig(@Validated @RequestBody EncryptEnvconfigVo envconfigVo) {
        if (StringUtils.isNotEmpty(envconfigVo.getConfigName()) && !envconfigService.checkConfigNameUnique(envconfigVo)) {
            return AjaxResult.error("新增加解密环境配置'" + envconfigVo.getConfigName() + "'失败，配置名称已存在");
        }
        EncryptEnvconfig envconfig = new EncryptEnvconfig();
        BeanUtils.copyProperties(envconfigVo, envconfig);
        envconfig.setCreatedBy(SecurityUtils.getUserId());
        return toAjax(envconfigService.save(envconfig));
    }

    @PreAuthorize("@ss.hasAnyPermi('encryption:envconfig:list,encryption:encrypt:all')")
    @GetMapping("getConfigList")
    public TableDataInfo getConfigList() {
        Long userId = getUserId();
        startPage();
        List<EncryptEnvconfig> configList = envconfigService.getConfigList(userId);
        if ("admin".equals(getUsername())) {
            configList = envconfigService.getAllConfigList();
        }
        return getDataTable(configList);
    }

    @PreAuthorize("@ss.hasPermi('encryption:encrypt:all')")
    @GetMapping("getAllConfigList")
    public TableDataInfo getAllConfigList() {
        startPage();
        List<EncryptEnvconfig> configList = envconfigService.getAllConfigList();
        return getDataTable(configList);
    }

    @PreAuthorize("@ss.hasPermi('encryption:envconfig:remove')")
    @Log(title = "加解密环境配置", businessType = BusinessType.DELETE)
    @DeleteMapping(value = {"/", "/{configId}"})
    public AjaxResult delEnvConfig(@PathVariable String[] configId) {
        Long userId = getUserId();
        return toAjax(envconfigService.delEnvConfigByIds(userId, configId));
    }

    @PreAuthorize("@ss.hasPermi('encryption:envconfig:list')")
    @GetMapping(value = {"/", "/{configId}"})
    public AjaxResult getEnvConfigById(@PathVariable(value = "configId") String configId) {
        Long userId = getUserId();
        EncryptEnvconfig envConfigById = envconfigService.getEnvConfigById(userId, configId);
        return AjaxResult.success(envConfigById);
    }

    @PreAuthorize("@ss.hasPermi('encryption:envconfig:edit')")
    @Log(title = "加解密环境配置", businessType = BusinessType.UPDATE)
    @PostMapping("updateEnvConfig")
    public AjaxResult updateEnvConfig(@RequestBody EncryptEnvconfigVo envconfigVo) {
        Long userId = getUserId();
        return toAjax(envconfigService.updateEnvConfig(userId, envconfigVo));
    }

}
